package me.jessyan.mvparms.demo.mvp.presenter;

import android.widget.Toast;

import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.RxLifecycleUtils;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.app.utils.RxUtils;
import me.jessyan.mvparms.demo.data.entity.MessageInfoEntity;
import me.jessyan.mvparms.demo.mvp.contract.RichEditorContract;
import me.jessyan.mvparms.demo.data.entity.TopResponse;
import me.jessyan.mvparms.demo.data.entity.UploadImgEntity;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;
import okhttp3.MultipartBody;

/**
 * @Author: 海洋
 * @Date: 2018/8/22
 * @Desc:
 */
public class RichEditorPresenter extends BasePresenter<RichEditorContract.Model, RichEditorContract.View> implements RichEditorContract.Presenter {


    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public RichEditorPresenter(RichEditorContract.Model model, RichEditorContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void uploadImage(MultipartBody.Part imgs) {
        RxUtils.<TopResponse<UploadImgEntity>>applySchedulers(mRootView,
                mRootView.getActivity().getString(R.string.is_uploading_now))
                .apply(mModel.uploadImage(imgs))
                .subscribe(new ErrorHandleSubscriber<TopResponse<UploadImgEntity>>(mErrorHandler) {
                    @Override
                    public void onNext(TopResponse<UploadImgEntity> response) {
                        mRootView.startInsertImageUrl(response.getData().getUrl().toString());
                    }

                    @Override
                    public void onError(Throwable t) {
                        mRootView.showMessage(mRootView.getActivity().getString(R.string.img_upload_fail));
                        Toast.makeText(mRootView.getActivity(), R.string.img_upload_fail, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    public void getMessageInfoById(String id) {
        RxUtils.<TopResponse<MessageInfoEntity>>applySchedulers(mRootView,
                mRootView.getActivity().getString(R.string.is_loading))
                .apply(mModel.getMessageInfoById(id))
                .subscribe(new ErrorHandleSubscriber<TopResponse<MessageInfoEntity>>(mErrorHandler) {
                    @Override
                    public void onNext(TopResponse<MessageInfoEntity> response) {
                        mRootView.startInsertHtml(response.getData().getContent());
                    }
                });
    }

    @Override
    public void addMessage(String title, String content) {
        RxUtils.<TopResponse<Object>>applySchedulers(mRootView,
                mRootView.getActivity().getString(R.string.is_uploading_now))
                .apply(mModel.addMessage(title, content))
                .subscribe(new ErrorHandleSubscriber<TopResponse<Object>>(mErrorHandler) {
                    @Override
                    public void onNext(TopResponse<Object> response) {
                        mRootView.showMessage("公告添加成功");
                    }
                });
    }

    @Override
    public void editMessage(String id, String title, String content) {
        RxUtils.<TopResponse<Object>>applySchedulers(mRootView,
                mRootView.getActivity().getString(R.string.is_uploading_now))
                .apply(mModel.editMessage(id, title, content))
                .subscribe(new ErrorHandleSubscriber<TopResponse<Object>>(mErrorHandler) {
                    @Override
                    public void onNext(TopResponse<Object> response) {
                        mRootView.showMessage("公告修改成功");
                    }
                });
    }
}
